package com.ruoyi.ejkgl.ejkdb.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ejkgl.ejkdb.domain.EjkdbDbmx;
import com.ruoyi.ejkgl.ejkdb.service.IEjkdbService;
import com.ruoyi.ejkgl.ejkwz.domain.Ejkwz;
import com.ruoyi.ejkgl.ejkwz.service.IEjkwzService;
import com.ruoyi.wzgl.wzck.domain.Wzck;
import com.ruoyi.wzgl.wzck.domain.WzckCkmx;
import com.ruoyi.wzgl.wzxx.domain.Wzxx;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.ejkgl.ejkdb.domain.Ejkdb;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 二级库调拨Controller
 *
 * @author huacai
 * @date 2024-12-10
 */
@RestController
@RequestMapping("/ejkgl/ejkdb")
public class EjkdbController extends BaseController
{
    @Resource
    private IEjkdbService ejkdbService;

    @Resource
    private IEjkwzService ejkwzService;

    /**
     * 查询二级库调拨列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Ejkdb ejkdb)
    {
        startPage();
        List<Ejkdb> list = ejkdbService.selectEjkdbList(ejkdb);
        return getDataTable(list);
    }

    /**
     * 导出二级库调拨列表
     */
    @Log(title = "二级库调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ejkdb ejkdb)
    {
        List<Ejkdb> list = ejkdbService.selectEjkdbList(ejkdb);
        ExcelUtil<Ejkdb> util = new ExcelUtil<Ejkdb>(Ejkdb.class);
        util.exportExcel(response, list, "二级库调拨数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Ejkdb> util = new ExcelUtil<Ejkdb>(Ejkdb.class);
        util.importTemplateExcel(response, "二级库调拨数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "二级库调拨", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Ejkdb> util = new ExcelUtil<Ejkdb>(Ejkdb.class);
        InputStream inputStream = file.getInputStream();
        List<Ejkdb> list = util.importExcel(inputStream );
        inputStream.close();
        int count = ejkdbService.batchInsertEjkdb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取二级库调拨详细信息
     */
    @GetMapping(value = "/{ejkdbId}")
    public AjaxResult getInfo(@PathVariable("ejkdbId") String ejkdbId)
    {
        return success(ejkdbService.selectEjkdbByEjkdbId(ejkdbId));
    }

    /**
     * 新增二级库调拨
     */
    @Log(title = "二级库调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ejkdb ejkdb)
    {
        return toAjax(ejkdbService.insertEjkdb(ejkdb));
    }

    /**
     * 修改二级库调拨
     */
    @Log(title = "二级库调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ejkdb ejkdb)
    {
        return toAjax(ejkdbService.updateEjkdb(ejkdb));
    }

    /**
     * 删除二级库调拨
     */
    @Log(title = "二级库调拨", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ejkdbIds}")
    public AjaxResult remove(@PathVariable String[] ejkdbIds)
    {
        return toAjax(ejkdbService.deleteEjkdbByEjkdbIds(ejkdbIds));
    }

    /**
     * 获得二级库调拨主键ID (UUID)
     */
    @GetMapping("/getEjkDbId")
    public AjaxResult getEjkDbId(){
        String getEjkDbId = IdUtils.fastSimpleUUID();
        return success(getEjkDbId);
    }

    /**
     * 确定调拨
     */
    @Log(title = "确定调拨", businessType = BusinessType.UPDATE)
    @PutMapping("/updateQdDb")
    public AjaxResult updateQdDb(@RequestBody Ejkdb ejkdb) {
        ejkdb.setDbZt("调拨完成");

        String dbdGq = ejkdb.getDbdGq(); //调拨到 (调拨到工区)

        //获取二级库调拨明细List
        List<EjkdbDbmx> ejkdbDbmxList = ejkdb.getEjkdbDbmxList();

        if (!preCheckEjkdbDbmxList(ejkdbDbmxList)) {
            return AjaxResult.error("调拨单中存在物资库存小于调拨数量, 请检查!");
        }
        //遍历出库明细
        for (EjkdbDbmx ejkdbDbmx : ejkdbDbmxList) {
            String ejkwzId = ejkdbDbmx.getEjkwzId(); // 二级库物资ID
            Integer dbSl = ejkdbDbmx.getDbSl(); // 调拨数量
            Integer wzxxId = ejkdbDbmx.getWzxxId(); // 物资ID

            // 根据wzxxId查询二级库物资
            Ejkwz ejkwzById = ejkwzService.selectEjkwzByEjkwzId(ejkwzId);
            Ejkwz bgqEjkwz = new Ejkwz();
            bgqEjkwz.setEjkwzId(ejkwzId); // 二级库物资ID
            bgqEjkwz.setKc(ejkwzById.getKc() - dbSl); // 本身的数量 - 调拨出去的数量
            ejkwzService.updateEjkwz(bgqEjkwz); // 调用二级库物资的修改方法

            // 查询二级库物资列表存不存在某个wzxx_id(物资信息ID) 和工区
            String sf = ejkwzService.selectCzEjkwzId(wzxxId, dbdGq);
            if ("false".equals(sf)) { // 调拨到的工区不存在这个物资
                Ejkwz ejkwz = new Ejkwz();
                ejkwz.setEjkwzId(IdUtils.fastSimpleUUID()); // 二级库物资ID
                ejkwz.setGq(dbdGq); // 工区
                ejkwz.setWzxxId(wzxxId); // 物资ID
                ejkwz.setKc(dbSl); // 库存
                ejkwzService.insertEjkwz(ejkwz); // 调用二级库物资的新增方法
            } else if ("true".equals(sf)) { // 调拨到的工区存在这个物资
                // 根据wzxxId和工区查询二级库物资
                Ejkwz ejkwzByWzxxIdOrGq = ejkwzService.selectEjkwzByWzxxId(wzxxId, dbdGq);
                Ejkwz ejkwz = new Ejkwz();
                // 既然这个物资本来就有, 就在new的实体类里面插入它的 ejkwzId(二级库物资ID)
                ejkwz.setEjkwzId(ejkwzByWzxxIdOrGq.getEjkwzId());
                ejkwz.setKc(ejkwzByWzxxIdOrGq.getKc() + dbSl); // 本身库存 + 调拨的数量
                ejkwzService.updateEjkwz(ejkwz); // 调用二级库物资的修改方法
            }
        }

        return toAjax(ejkdbService.updateEjkdb(ejkdb));
    }

    // 判断库存是否小于调拨数量
    private boolean preCheckEjkdbDbmxList(List<EjkdbDbmx> ejkdbDbmxList) {
        for (EjkdbDbmx ejkdbDbmx : ejkdbDbmxList) {
            String ejkwzId = ejkdbDbmx.getEjkwzId(); // 二级库物资ID
            Integer dbSl = ejkdbDbmx.getDbSl(); // 调拨数量

            // 根据 二级库物资ID 查询二级库物资
            Ejkwz ejkwzById = ejkwzService.selectEjkwzByEjkwzId(ejkwzId);
            if (ejkwzById == null || ejkwzById.getKc() < dbSl) { // 如果库存 小于 调拨数量
                return false;
            }
        }
        return true;
    }

}
